package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Patient;
import com.hp.service.PatientService;
import com.hp.util.ResultJson;
import com.hp.util.Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {
    @Resource
    private PatientService patientService;
    @RequestMapping("/findAll")
    private void findAll(Patient patient, @RequestParam(value = "page",defaultValue = "1")Integer page,
                         @RequestParam(value = "limit",defaultValue = "10")Integer limit, HttpServletResponse response){
        System.out.println(patient);
        PageHelper.startPage(page,limit);
        List<Patient> list = patientService.findAll(patient);
        System.out.println(list);
        if (list.size() != 0) {
            Utils.returnJson(ResultJson.success(new PageInfo<>(list)), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/findById")
    private void findById(Patient patient, HttpServletResponse response){
        System.out.println(patient);
        Patient patient1 = patientService.findById(patient);
        System.out.println(patient1);
        if (patient1 != null) {
            Utils.returnJson(ResultJson.success(patient1), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/findByIdAll")
    private void findByIdAll(Patient patient, HttpServletResponse response){
        System.out.println(patient);
        Patient patient1 = patientService.findByIdAll(patient);
        System.out.println(patient1);
        if (patient1 != null) {
            Utils.returnJson(ResultJson.success(patient1), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/delete")
    private void delete(String[] id, HttpServletResponse response){
        int i  = patientService.delete(id);
        if (i > 0) {
            Utils.returnJson(ResultJson.success(i), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/add")
    private void add(Patient patient, HttpServletResponse response){
        System.out.println(patient);
        Patient patient1 = patientService.findById(patient);
//        System.out.println(patient1.getOutpatientQueue().getStatus());
        if(patient1.getId_card().equals(patient.getId_card())){
            Utils.returnJson(ResultJson.error(505,"ACTIVATED"),response);
        }else {
            if (patient1.getCard_id().equals(patient.getCard_id())) {
                Utils.returnJson(ResultJson.error(505, "COVER"), response);
            }else {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String create_datetime = sdf.format(date);
                patient.setCreate_datetime(create_datetime);
                int i  = patientService.add(patient);
                if(i == 0){
                    Utils.returnJson(ResultJson.error(505,"FAIL"), response);
                }else {
                    if(i > 0 && !patient1.getCard_id().equals(patient.getCard_id())) {
                        int j = patientService.addIdCadr(patient);
                    }
                }
            }
        }
    }
    @RequestMapping("/update")
    private void update(Patient patient, HttpServletResponse response){
        int i  = patientService.update(patient);
        if (i > 0) {
            Utils.returnJson(ResultJson.success(i), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    //查询已挂号患者
    @RequestMapping("selectDan")
    private void selectDan(HttpServletRequest request, HttpServletResponse response){
        String email= (String) request.getSession().getAttribute("email");
        List<Patient> list=patientService.selectDan(email);
        ResultJson success = ResultJson.success(list);
        Utils.returnJson(success,response);
    }
}
